package View;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class Revise {
    JFrame jf = new JFrame("修改学生");
    JButton xg=new JButton(      "修改该学生信息");
    public Revise() {
        Box b= Box.createVerticalBox();
        Box h=Box.createHorizontalBox();
        JLabel jl=new JLabel("请输入要修改学生的学号: ");
        JTextField jt=new JTextField(15);
        Box b2= Box.createHorizontalBox();
        Box b3= Box.createHorizontalBox();
        JLabel ts=new JLabel("提示");
        ts.setForeground(Color.red);
        h.add(jl);
        b2.add(Box.createHorizontalStrut(15));
        h.add(jt);

        b2.add(xg);
        b3.add(ts);

        b.add(h);
        b.add(Box.createVerticalStrut(10));
        b.add(b2);
        b.add(Box.createVerticalStrut(7));
        b.add(b3);
        jf.add(b);
        jf.setSize(388, 130);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        jf.setResizable(false);
        jf.setVisible(true);
        jf.setLayout(new FlowLayout(FlowLayout.CENTER));
        xg.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (jt.getText().equals("")) {
                    ts.setText("信息不能为空");
                } else {
                    String ts1=jt.getText();
                    System.out.println(ts1);
                    try {
                        Class.forName("com.mysql.cj.jdbc.Driver");
                        Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/lxt?autoReconnect=true", "root", "111111");
                        Statement statement = c.createStatement();
                        ResultSet result = statement.executeQuery("select *from evaluate where 学号='"+ts1+"'");
           if (result.next()) {
               String name=result.getString("密码");

               String passwd=result.getString("姓名");

               JFrame zhu = new JFrame("请输入要修改的信息");
               Box s = Box.createVerticalBox();
               Box h1 = Box.createHorizontalBox();
               JLabel xh = new JLabel("请输入要修改的学号;  ");
               JTextField xhh = new JTextField(15);
               Box h2 = Box.createHorizontalBox();
               xhh.setText(ts1);
               JLabel xm = new JLabel("请输入要修改的密码;  ");
               JTextField xmm = new JTextField(15);
               xmm.setText(name);
               Box h3 = Box.createHorizontalBox();
               JLabel ma = new JLabel("请输入要修改的姓名;  ");
               JTextField maa = new JTextField(15);
               Box h4=Box.createHorizontalBox();
               maa.setText(passwd);
               JButton xgg=new JButton("确认修改");
               Box h5=Box.createHorizontalBox();
               JLabel ts2=new JLabel();




               h1.add(xh);
               h1.add(xhh);

               h2.add(xm);
               h2.add(xmm);

               h3.add(ma);
               h3.add(maa);

               h4.add(xgg);
               h5.add(ts2);
               s.add(h1);
               s.add(h2);
               s.add(h3);
               s.add(h4);
               s.add(h5);
               zhu.add(s);

               zhu.setSize(388, 166);
               zhu.setLocationRelativeTo(null);
               zhu.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
               zhu.setResizable(false);
               zhu.setVisible(true);
               zhu.setLayout(new FlowLayout(FlowLayout.CENTER));
               result.close();
               statement.close();
               c.close();
               xgg.addActionListener(new ActionListener() {
                   @Override
                   public void actionPerformed(ActionEvent e) {
                       String g1=xhh.getText();
                       String g2=xmm.getText();
                       String g3=maa.getText();

                       try {
                           try {
                               Class.forName("com.mysql.cj.jdbc.Driver");
                           } catch (ClassNotFoundException ex) {
                               ex.printStackTrace();
                           }
                           Connection c= DriverManager.getConnection("jdbc:mysql://localhost:3306/lxt?autoReconnect=true","root","111111");
                           Statement statement=c.createStatement();
                           ResultSet result=statement.executeQuery("select *from evaluate where 学号='"+g1+"'");
                    if (result.next()){
                        int re = statement.executeUpdate("UPDATE evaluate SET 学号 = '" + g1 + "', 密码 = '" + g2 + "', 姓名 = '" + g3 + "'" +
                                "WHERE 学号 = '" + g1 + "'");
                        ts.setText("修改成功");
                        zhu.dispose();
                        new ZhuJieMian().setPagnow(100);
                        ZhuJieMian.reloadTable();
                    }else {
                                ts2.setText("此学号不存在");


                    }
                       } catch (SQLException ex) {
                           ex.printStackTrace();
                       }

                   }
               });
           }else {
               ts.setText("请输入正确的学号");
           }
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    } catch (ClassNotFoundException classNotFoundException) {
                        classNotFoundException.printStackTrace();
                    }


                }
                }
        });
    }

    public static void main(String[] args) {
        new Revise();
    }
}